/**
 * 添加或者修改页面
 */
var GivenPublicNoticeInfoDlg = {
    data: {
        name: "",
        uploadtime: "",
        introduction: "",
        type: "",
        coverImg: "",
        homePageIntroduction: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: ""
    }
};

layui.use(['form', 'admin', 'ax','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload =layui.upload;

    //让当前iframe弹层高度适应
    //admin.iframeAuto();

    //实例化编辑器
    var ue = UE.getEditor('introduction', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/fastDFS/ueditor/upload';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/fastDFS/ueditor/upload';
        } else if (action === 'uploadvideo') {
            console.log('上传视频')
            return Feng.ctxPath + '/fastDFS/ueditor/upload';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };

    //上传标题图片（多图）
    var url = '';
    upload.render({
        elem: '#uploadImagesTitle',
        url: '/fastDFS/uploadImageThumbnail',
        multiple: true,
        data: {
            width: 750,
            height: 450
        },
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#imagesTitlePrew').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" style="width:100%;height: 185px;">')
            });
        },
        done: function (res) {
            //上传完毕回调
            layer.closeAll('loading'); //关闭loading

            //上传成功
            if (res.code === 200) {
                url += res.data.imgUrlFull + ','
            } else {
                return layer.msg('上传失败');
            }
        },
        allDone: function () {
            $("#url").attr("value", url);
        },
    });

//上传首页图片
    upload.render({
        //绑定元素
        elem: '#uploadPhoto',
        //上传接口
        // url: '/fastDFS/upload',
        url: '/fastDFS/uploadImageThumbnail',
        accept: 'file',
        data: {
            width: 1080,
            height: 582
        },
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#photoView').attr('src', result);
            });

            layer.load();
        },
        done: function (res) {
            //上传完毕回调
            layer.closeAll('loading'); //关闭loading

            //假设code=200代表上传成功
            if (res.code === 200) {
                $("#coverImg").attr("value", res.data.imgUrlFull);
            } else {
                return layer.msg('上传失败');
            }
        },
        error: function () {
            layer.closeAll('loading'); //关闭loading
            //请求异常回调
            //当上传失败时，你可以生成一个“重新上传”的按钮，点击该按钮时，执行 upload() 方法即可实现重新上传
            //获取当前触发上传的元素，一般用于 elem 绑定 class 的情况，注意：此乃 layui 2.1.0 新增
            var item = this.item;
            console.log(item.val())
        }
    });
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/givenPublicNotice/addItem", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

});